#!/usr/bin/env groovy

pipeline {
  agent none
  stages {

    stage('Build') {
      steps {
        script {
          stage('sitl package') {
            node {
              docker.image("px4io/px4-dev-ros:2018-07-19").inside('-e CCACHE_BASEDIR=$WORKSPACE -v ${CCACHE_DIR}:${CCACHE_DIR}:rw -e HOME=$WORKSPACE') {
                stage("sitl package") {
                  try {
                    checkout(scm)
                    sh('export')
                    sh('make distclean')
                    sh "ccache -z"
                    sh('make posix_sitl_default')
                    sh "ccache -s"
                    sh('make posix_sitl_default sitl_gazebo')
                    sh "ccache -s"
                    sh('make posix_sitl_default package')
                    stash(name: "px4_sitl_package", includes: "build/posix_sitl_default/*.bz2")
                  }
                  catch (exc) {
                    throw (exc)
                  }
                  finally {
                    sh('make distclean')
                  }
                }
              }
            }
          }
        } // script
      } // steps
    } // Build

    stage('ROS Tests') {
      steps {
        script {
          def missions = [
            [
              name: "VTOL standard",
              test: "mavros_posix_test_mission.test",
              mission: "vtol_new_1",
              vehicle: "standard_vtol"
            ],
            [
              name: "VTOL tailsitter",
              test: "mavros_posix_test_mission.test",
              mission: "vtol_new_1",
              vehicle: "tailsitter"
            ],
            [
              name: "VTOL tiltrotor",
              test: "mavros_posix_test_mission.test",
              mission: "vtol_new_1",
              vehicle: "tiltrotor"
            ],
            [
              name: "MC box",
              test: "mavros_posix_test_mission.test",
              mission: "multirotor_box",
              vehicle: "iris"
            ],
            [
              name: "MC offboard att",
              test: "mavros_posix_tests_offboard_attctl.test",
              mission: "",
              vehicle: "iris"
            ],
            [
              name: "MC offboard pos",
              test: "mavros_posix_tests_offboard_posctl.test",
              mission: "",
              vehicle: "iris"
            ]
          ]
        
          def test_nodes = [:]
          for (def i = 0; i < missions.size(); i++) {
            test_nodes.put(missions[i].name, createTestNode(missions[i]))
          }

          parallel test_nodes
        } // script
      } // steps
    } // ROS Tests

  } //stages
  environment {
    CCACHE_DIR = '/tmp/ccache'
    CI = true
  }
  options {
    buildDiscarder(logRotator(numToKeepStr: '10', artifactDaysToKeepStr: '30'))
    timeout(time: 60, unit: 'MINUTES')
  }
} // pipeline

def createTestNode(Map test_def) {
  return {
    node {
      cleanWs()
      docker.image("px4io/px4-dev-ros:2018-03-30").inside('-e HOME=${WORKSPACE}') {
        stage(test_def.name) {
          try {
            sh('export')            
            unstash('px4_sitl_package')
            sh('tar -xjpvf build/posix_sitl_default/px4-posix_sitl_default*.bz2')
            sh('px4-posix_sitl_default*/px4/test/rostest_px4_run.sh ' + test_def.test + ' mission:=' + test_def.mission + ' vehicle:=' + test_def.vehicle)
            sh('px4-posix_sitl_default*/px4/Tools/ecl_ekf/process_logdata_ekf.py .ros/rootfs/fs/microsd/log/*/*.ulg')
            }
          catch (exc) {
            archiveArtifacts(allowEmptyArchive: false, artifacts: '.ros/**/*.ulg, .ros/**/rosunit-*.xml, .ros/**/rostest-*.log')
            throw (exc)
          }
          finally {
            sh('px4-posix_sitl_default*/px4/Tools/upload_log.py -q --description "${JOB_NAME}: ${STAGE_NAME}" --feedback "${JOB_NAME} ${CHANGE_TITLE} ${CHANGE_URL}" --source CI .ros/rootfs/fs/microsd/log/*/*.ulg')
            archiveArtifacts(allowEmptyArchive: false, artifacts: '.ros/**/*.pdf, .ros/**/*.csv')
          }
        }
      }
      cleanWs()
    }
  }
}